package cn.itcast.controller.system;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.RoleService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/system/role")
public class RoleController extends BaseController {


    @Autowired
    private RoleService roleService;
    @Autowired
    private ModuleService moduleService;

    @RequestMapping(value = "/list",name = "分页展示角色数据列表展示") // page代表当前的页码  page=2
    public String findAll(@RequestParam(value = "page",defaultValue = "1") Integer pageNum , @RequestParam(defaultValue = "10")Integer pageSize){
        PageInfo pageInfo = roleService.findPage(getCompanyId(),pageNum,pageSize);
        request.setAttribute("page",pageInfo); //这里的page名称不能改，因为页面要的就是page这个名称
        return "system/role/role-list";
    }

    @RequestMapping(value = "/toAdd",name = "跳转到新增角色页面")
    public String toAdd(){

        return "system/role/role-add";
    }

    @RequestMapping(value = "/edit",name = "保存角色信息")
    public String edit(Role role){

        if(StringUtils.isNotEmpty(role.getId())){
//           id不为空，应该执行修改方法
            roleService.update(role);
        }else{
            role.setId(UUID.randomUUID().toString());
            role.setCompanyId(getCompanyId());
            role.setCompanyName(getCompanyName());
            roleService.save(role);
        }
        return "redirect:/system/role/list.do"; // 重定向到列表页面
    }
    @RequestMapping(value = "/toUpdate",name = "跳转到修改角色页面")
    public String toUpdate(String id){
        Role role = roleService.findById(id);
        request.setAttribute("role",role);

//        新增和修改页面共用一个
        return "system/role/role-add";
    }
    @RequestMapping(value = "/delete",name = "删除角色数据")
    public String delete(String id){
        roleService.deleteById(id);
        return "redirect:/system/role/list.do"; // 重定向到列表页面
    }

    @RequestMapping(value = "/roleModule",name = "进入到角色分配权限页面")
    public String roleModule(String roleid){
        Role role = roleService.findById(roleid);
        request.setAttribute("role",role);
        return "system/role/role-module";
    }

   /* [
    { id:11, pId:1, name:"随意勾选 1-1"},
    { id:111, pId:11, name:"随意勾选 1-1-1"},
    { id:112, pId:11, name:"随意勾选 1-1-2"}
    ]*/
    @RequestMapping(value = "/getZtreeNodes",name = "获取到ztree需要的节点数据")
    @ResponseBody //把数据转成json并且返回到浏览器上
    public List<Map> getZtreeNodes(String roleid){
        List<Map> mapList = new ArrayList<>(); //用来构建返回的数据
//        1、查询所有的模块数据
        List<Module> moduleList = moduleService.findAll();
//        2、查询当前角色拥有的模块权限id  List<String> moduleIdList  [121212,3423434]
       List<String> moduleIdList = moduleService.findModuleIdsByRoleId(roleid);

        Map map;
        for (Module module : moduleList) {
            map = new HashMap();
            map.put("id",module.getId());
            map.put("pId",module.getParentId());
            map.put("name",module.getName());
            if(moduleIdList.contains(module.getId())){
                map.put("checked",true);            //
            }
            mapList.add(map);
        }
        return mapList;
    }

    @RequestMapping(value = "/updateRoleModule",name = "完成角色分配权限方法")
    public String updateRoleModule(String roleid,String moduleIds){ // moduleIds "1212,353543,1312"
        roleService.updateRoleModule(roleid,moduleIds);
        return "redirect:/system/role/list.do"; //重定向到角色列表页面
    }

}
